<?php

namespace app\admin\controller;

use think\Controller;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use app\common\model\UserModel;
use app\common\model\SmsModel;

AlibabaCloud::accessKeyClient('LTAIz4NFeJkswAJA', '0GKSQa9pTPeguayrWAtLawxTeKQRvU')
    ->regionId('cn-hangzhou')
    ->asDefaultClient();

class AccountController extends Controller
{
    /**
     * 登入
     */
    public function login()
    {
        if ($this->request->isGet()) {
            return view();
        } else {
            $post = $this->request->post();
            if (!captcha_check($post['vercode'])) {
                return zbcjson(false, '验证码错误', 5000);
            }
            $user = UserModel::get([
                'phone' => $post['phone'],
                'password' => md5($post['password'])
            ]);
            if (!empty($user)) {
                cookie('zbc', encrypt(base64_encode($user->toJson())));
                return zbcjson(true);
            } else {
                return zbcjson(false, '用户密码错误', 200);
            }
        }
    }

    /**
     * 登出
     */
    public function logout()
    {
        cookie('zbc',null);
        return zbcjson(true);
    }

    /**
     * 注册
     */
    public function register()
    {
        if ($this->request->isGet()) {
            return view();
        } else {
            $user = new UserModel();
            $user->phone = $this->request->post('cellphone');
            $user->password = md5($this->request->post('password'));
            $user->nickname = $this->request->post('nickname');
            $result = $user->save();
            if ($result > 0) {
                return zbcjson(true);
            } else {
                return zbcjson(false, '插入错误', 101);
            }
        }
    }

    /**
     * 用户信息
     */
    public function userInfo()
    {
        $data['user'] = $this->request->user;
        return view("", $data);
    }

    /**
     * 修改密码
     */
    public function password()
    {
        return view();
    }

    /**
     * 忘记密码
     */
    public function forget()
    {
        return view();
    }

    /**
     * 发送验证码
     */
    public function sendCode()
    {
        $phone = $_POST['phone'];
        $code = mt_rand(10000, 99999);
        try {
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                // ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'PhoneNumbers' => $phone,
                        'SignName' => "真不错",
                        'TemplateCode' => "SMS_161597537",
                        'TemplateParam' => "{code:$code}",
                    ],
                ])
                ->request();
            $data = $result->toArray();

            if ($data['Code'] == "OK") {
                $sms = new SmsModel();
                $sms->phone = $phone;
                $sms->bizid = $data['BizId'];
                $sms->smsCode = $code;
                $sms->code = $data['Code'];
                $sms->message = $data['Message'];
                $sms->requestId = $data['RequestId'];
                $sms->save();
                return zbcjson(true);
            } else {
                return zbcjson(false, $data['message'], 500);
            }
        } catch (ClientException $e) {
            return zbcjson(false, $e->getErrorMessage(), 500);
        } catch (ServerException $e) {
            return zbcjson(false, $e->getErrorMessage(), 500);
        }
    }
}
